﻿using System.Threading.Tasks;
using client.helpers;
using client.models.Net;
using Newtonsoft.Json.Linq;

namespace client.models
{
    public class ServiceApi
    {
        public async Task<UserInfo> Login(string user, string password)
        {
            UserInfo info = null;

            using (NetClient client = new NetClient())
            {
                if (await client.ConnectAsync(Constant.ServiceIP, Constant.ServicePort, 5))
                {
                    var loginData = new { alias = user, pwd = AESHelper.Encrypt(password) };
                    var response = await client.SendRequestAsync(Constant.NetCmd_Login, loginData);

                    if (response != null && response.result)
                    {
                        var userinfo = response.data as JObject;
                        if (userinfo != null)
                        {
                            info = new UserInfo()
                            {
                                Name = userinfo["name"].ToString(),
                                Alias = userinfo["alias"].ToString(),
                                TeamRole = userinfo["team_role"].ToString(),
                            };
                        }
                    }
                }
            }

            return info;
        }
    }
}
